package org.lilred.nzbberry.views;

import org.lilred.nzbberry.config.Resizer;
import org.lilred.nzbberry.controllers.PageController;
import org.lilred.nzbberry.ui.BannerButton;
import org.lilred.nzbberry.ui.BannerField;
import org.lilred.nzbberry.ui.BannerManager;
import org.lilred.nzbberry.ui.FooterField;
import org.lilred.nzbberry.ui.IndicatorField;
import org.lilred.nzbberry.util.ScaleImage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ButtonField;

public class AppManager extends Manager {
	private BannerField bannerField;
	private FooterField footerField;
	private Manager currentView;

	private boolean menusEnabled;
	Bitmap bannerLogoBitmap = new Bitmap(0, 0);
	Bitmap bannerBitmap = new Bitmap(0, 0);
	Bitmap footerBitmap = new Bitmap(0, 0);
	Bitmap logoBitmap = new Bitmap(0, 0);

	public IndicatorField queueIndicatorField = new IndicatorField();
	BannerManager bannerManager = new BannerManager();
	public BannerButton searchButton = new BannerButton("search.png", "search-focus.png", "Search for NZBs online");
	public BannerButton queueButton = new BannerButton("queue.png", "queue-focus.png", "View download queue");
	public BannerButton historyButton = new BannerButton("history.png", "history-focus.png", "View download history");
	public BannerButton configButton = new BannerButton("config.png", "config-focus.png", "Edit configuration and view warnings");

	public AppManager(boolean menusEnabled) {
		super(USE_ALL_HEIGHT | USE_ALL_WIDTH);
		this.menusEnabled = menusEnabled;
		EncodedImage e;
		e = EncodedImage.getEncodedImageResource("footer.png");
		e = ScaleImage.toWidth(e, Resizer.resize(474));
		footerBitmap = e.getBitmap();
		e = EncodedImage.getEncodedImageResource("nzbberry-small.png");
		e = ScaleImage.toWidth(e, Resizer.resize(66));
		bannerLogoBitmap = e.getBitmap();
		e = EncodedImage.getEncodedImageResource("banner54.png");
		e = ScaleImage.toWidth(e, Resizer.resize(474));
		bannerBitmap = e.getBitmap();
		e = EncodedImage.getEncodedImageResource("nzbBerryLogo.png");
		e = ScaleImage.toWidth(e, (int) (Display.getWidth() * .4));
		logoBitmap = e.getBitmap();
		bannerField = new BannerField();
		add(bannerField);
		footerField = new FooterField();
		add(footerField);
		add(bannerManager);
		if (menusEnabled) {
			add(queueIndicatorField);
			bannerManager.add(queueButton);
			bannerManager.add(historyButton);
			bannerManager.add(searchButton);
			bannerManager.add(configButton);
		}
	}

	public void setPageView(Manager view) {
		if (currentView == view) return;
		if (currentView != null && currentView.getManager() == this) delete(currentView);
		currentView = view;
		add(currentView);
	}

	public Manager getPageView() { return currentView; }

	public void updateBanner(String line1, String line2) {
		bannerField.updateBanner(line1, line2);
	}

	public void updateFooter(String text) {
		footerField.updateFooter(text);
	}

	protected void sublayout(int width, int height) {
		int verticalExtent = Display.getHeight();
		int contentHeight = verticalExtent - bannerBitmap.getHeight() - footerBitmap.getHeight() - Resizer.resize(6); 
		if (currentView != null && currentView.getManager() == this) {
			layoutChild(currentView, width - 2 * Resizer.resize(6), contentHeight);
			setPositionChild(currentView, Resizer.resize(6), Resizer.resize(57));
		}
		//Banner
		layoutChild(bannerField, width - 2 * Resizer.resize(3), Resizer.resize(54));
		setPositionChild(bannerField, Resizer.resize(3), Resizer.resize(3));
		//Banner Buttons
		if (menusEnabled) {
			layoutChild(bannerManager, width, Resizer.resize(60));
			setPositionChild(bannerManager, 0, Resizer.resize(10));
			layoutChild(footerField, width - Resizer.resize(100), Resizer.resize(30));
			setPositionChild(footerField, width - footerField.getWidth() - Resizer.resize(15),verticalExtent - Resizer.resize(2) - footerField.getHeight());
			layoutChild(queueIndicatorField, queueIndicatorField.getPreferredWidth(), queueIndicatorField.getPreferredHeight());
			setPositionChild(queueIndicatorField, Resizer.resize(10), verticalExtent - Resizer.resize(9) - queueIndicatorField.getHeight());
		}
		setExtent(width, verticalExtent);
	}
	protected void paint(Graphics g) {
		g.setBackgroundColor(0xb1b1b1);
		g.clear();

		//Draw the rounded rectangle that encompasses the entire screen
		g.setColor(0xd0d0d0);
		g.fillRoundRect(Resizer.resize(3), //X pos 
				Resizer.resize(3), //Y pos
				getWidth() - 2 * Resizer.resize(3), //Width 
				getHeight() - 2 * Resizer.resize(3), //Height
				3, 3); //Arc width & height

		//Draw the banner background
		g.drawBitmap(Resizer.resize(3), //X pos 
				Resizer.resize(3), //Y pos
				bannerBitmap.getWidth(), //Width 
				bannerBitmap.getHeight(), //Height
				bannerBitmap, 0, 0);

		//Draw logo at the top
		g.drawBitmap(getWidth() / 2 - bannerLogoBitmap.getWidth() / 2, //X pos (centered) 
				Resizer.resize(13), //Y pos
				bannerLogoBitmap.getWidth(), //Width
				bannerLogoBitmap.getHeight(), //Height
				bannerLogoBitmap, 0, 0);

		//Draw footer background
		g.drawBitmap(Resizer.resize(3), //X pos 
				getVisibleHeight() - Resizer.resize(3) - footerBitmap.getHeight(), //Y pos 
				footerBitmap.getWidth(), //Width
				footerBitmap.getHeight(), //Height
				footerBitmap, 0, 0);

		//Draw the nzbBerry logo in the center of the screen
		g.setGlobalAlpha(25);
		g.drawBitmap(getWidth() / 2 - logoBitmap.getWidth() / 2, 
				getVisibleHeight() / 2 - logoBitmap.getHeight() / 2, 
				getWidth(), 
				getHeight(), 
				logoBitmap, 0, 0);
		g.setGlobalAlpha(255);
		g.setColor(0x000000);

		super.paint(g);
	}
}
